package private

import (
	"dc-go/global"
	"dc-go/model"
	"dc-go/utils"

	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
)

type ApiOrg struct{}

func (a *ApiOrg) GetOrgList(c *gin.Context) {
	var requestOrgList model.RequestGetOrgList
	if err := model.RequestShouldBindJSON(c, &requestOrgList); err != nil {
		return
	}
	if orgList, total, err := servicePrivate.ServiceOrg.GetOrgList(requestOrgList); err != nil {
		global.DcLogger.Error("获取组织列表失败! ", zap.Any("err", err))
		model.ResponseErrorMessage("获取组织列表失败, "+err.Error(), c)
	} else {
		model.ResponseSuccessData(model.ResponsePage{
			Records:  orgList,
			Page:     requestOrgList.Page,
			PageSize: requestOrgList.PageSize,
			Total:    total,
		}, c)
	}
}

func (a *ApiOrg) QueryOrgById(c *gin.Context) {
	var toQueryId model.RequestQueryById
	if err := model.RequestShouldBindJSON(c, &toQueryId); err != nil {
		return
	}
	if org, err := servicePrivate.ServiceOrg.QueryOrgById(toQueryId.Id); err != nil {
		global.DcLogger.Error("查找组织失败! ", zap.Any("err", err))
		model.ResponseErrorMessage("查找组织失败, "+err.Error(), c)
	} else {
		model.ResponseSuccessMessageData(gin.H{"records": org}, "查找组织成功! ", c)
	}
}

func (a *ApiOrg) QueryRoleByOrg(c *gin.Context) {
	var orgCode model.RequestOrgCode
	if err := model.RequestShouldBindJSON(c, &orgCode); err != nil {
		return
	}
	if roleList, err := servicePrivate.ServiceOrg.QueryRoleByOrg(&orgCode); err != nil {
		global.DcLogger.Error("查找组织角色失败! ", zap.Any("err", err))
		model.ResponseErrorMessage("查找组织角色失败, "+err.Error(), c)
	} else {
		model.ResponseSuccessData(gin.H{"records": roleList}, c)
	}
}

func (a *ApiOrg) RemoveOrgRole(c *gin.Context) {
	var toDeleteOrgRole model.RequestOrgRole
	if err := model.RequestShouldBindJSON(c, &toDeleteOrgRole); err != nil {
		return
	}
	if err := servicePrivate.ServiceOrg.RemoveOrgRole(&toDeleteOrgRole); err != nil {
		global.DcLogger.Error("移除组织角色失败! ", zap.Any("err", err))
		model.ResponseErrorMessage("移除组织角色失败, "+err.Error(), c)
	} else {
		global.DcLogger.Warn(utils.GetEmpeeAcct(c) + "移除组织角色成功! ")
		model.ResponseSuccessMessage("移除组织角色成功! ", c)
	}
}

func (a *ApiOrg) AddOrgRole(c *gin.Context) {
	var toAddOrgRole model.RequestOrgRoleAdd
	if err := model.RequestShouldBindJSON(c, &toAddOrgRole); err != nil {
		return
	}
	if err := servicePrivate.ServiceOrg.AddOrgRole(&toAddOrgRole); err != nil {
		global.DcLogger.Error("添加组织角色失败! ", zap.Any("err", err))
		model.ResponseErrorMessage("添加组织角色失败, "+err.Error(), c)
	} else {
		model.ResponseSuccessMessage("添加组织角色成功! ", c)
	}
}
